package com.youlai.boot.system.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.form.ExamQuestionForm;
import com.youlai.boot.system.model.query.ExamQuestionQuery;
import com.youlai.boot.system.model.vo.ExamQuestionVO;
import com.youlai.boot.system.service.ExamQuestionService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

/**
 * 试题列表前端控制层
 *
 * @author youlaitech
 * @since 2025-07-07 09:05
 */
@Tag(name = "试题列表接口")
@RestController
@RequestMapping("/api/v1/exam-question")
@RequiredArgsConstructor
public class ExamQuestionController  {

    private final ExamQuestionService examQuestionService;

    @Operation(summary = "试题列表分页列表")
    @GetMapping("/page")
    public PageResult<ExamQuestionVO> getExamQuestionPage(ExamQuestionQuery queryParams ) {
        IPage<ExamQuestionVO> result = examQuestionService.getExamQuestionPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增试题列表")
    @PostMapping
    public Result<Void> saveExamQuestion(@RequestBody @Valid ExamQuestionForm formData ) {
    	
    	System.out.println(formData.toString());
    	
        boolean result = examQuestionService.saveExamQuestion(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取试题列表表单数据")
    @GetMapping("/{id}/form")
    public Result<ExamQuestionForm> getExamQuestionForm(
        @Parameter(description = "试题列表ID") @PathVariable Long id
    ) {
        ExamQuestionForm formData = examQuestionService.getExamQuestionFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改试题列表")
    @PutMapping(value = "/{id}")
    public Result<Void> updateExamQuestion(
            @Parameter(description = "试题列表ID") @PathVariable Long id,
            @RequestBody @Validated ExamQuestionForm formData
    ) {
        boolean result = examQuestionService.updateExamQuestion(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除试题列表")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteExamQuestions(
        @Parameter(description = "试题列表ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = examQuestionService.deleteExamQuestions(ids);
        return Result.judge(result);
    }
}
